package com.dcd.controller;

import com.dcd.pojo.Car_series;
import com.dcd.pojo.User;
import com.dcd.service.BrandService;
import com.dcd.service.Car_seriesService;
import com.dcd.util.AesUtil;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/Car_seriesController")
public class Car_seriesController {

    @Autowired
    private AesUtil aesUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Car_seriesService car_seriesService;
    @Autowired
    private BrandService brandService;

    /**
     * 选车
     *
     * @param conditions_str 条件字符串(3-100,+-3-3.0,3.5-3-4-7+-pageIndex)
     * @return
     */
    @RequestMapping("/vehicle_selection_by_conditions/{conditions_str}")
    public Map<String, Object> vehicle_selection_by_conditions(@PathVariable String conditions_str) {
        return car_seriesService.vehicle_selection_by_conditions(encapsulation_map(conditions_str));
    }

    /**
     * 查询指定条件车图
     *
     * @param conditions_str 条件字符串(3-100,+-3-3.0,3.5-3-4-7+-pageIndex)
     * @return
     */
    @RequestMapping("/select_all_series_images_by_conditions/{conditions_str}")
    public Map<String, Object> select_all_series_images_by_conditions(@PathVariable String conditions_str) {
        return car_seriesService.select_all_series_images_by_conditions(encapsulation_map(conditions_str));
    }

    /**
     * 封装查询车系/车系图片的条件map
     *
     * @param conditions_str
     * @return
     */
    private Map<String, String> encapsulation_map(String conditions_str) {
        String[] split = conditions_str.split("-");
        HashMap<String, String> conditions = new HashMap<>();
        conditions.put("brand_id", split[0]);/*品牌id*/
        conditions.put("min_price", !split[1].equals("x") ? split[1].split(",")[0] : "x");/*最低价*/
        conditions.put("max_price", !split[1].equals("x") ? split[1].split(",")[1] : "x");/*最高价*/
        conditions.put("level", split[2]);/*等级*/
        conditions.put("min_points", !split[3].equals("x") ? split[3].split(",")[0] : "x");/*最低懂车分*/
        conditions.put("max_points", !split[3].equals("x") ? split[3].split(",")[1] : "x");/*最高懂车分*/
        conditions.put("energy", split[4]);/*能源id*/
        conditions.put("different_countries", split[5]);/*国别id*/
        conditions.put("seat_count", split[6]);/*座椅数量*/
        conditions.put("pageIndex", split[7]);
        return conditions;
    }

    /**
     * 查询历史浏览车系
     *
     * @param history_browsing_car_series_ids 历史浏览id
     * @return
     */
    @RequestMapping("/select_history_browsing_car_series")
    public List<Car_series> select_history_browsing_car_series(@CookieValue(value = "history_browsing_car_series_ids", required = false) Cookie history_browsing_car_series_ids) {
        if (history_browsing_car_series_ids != null) {
            String[] car_series_ids = history_browsing_car_series_ids.getValue().split("-");
            return car_seriesService.select_history_browsing_car_series(car_series_ids);
        }
        return null;
    }

    /**
     * 查询指定用户关注车系
     *
     * @param token 用户token
     * @return
     */
    @RequestMapping("/select_my_follow_car_series/{token}")
    private Object select_my_follow_car_series(@PathVariable String token) throws Exception {
        Object result = redisTemplate.opsForValue().get(aesUtil.decrypt(token));
        if (result != null) {
            User user = (User) redisTemplate.opsForValue().get(result);
            return car_seriesService.select_my_follow_car_series(user.getUser_id());
        }
        /*登录过期*/
        return "{\"result\":\"no_line\"}";
    }

    /**
     * 查询首页头部车系
     *
     * @param different_countries 根据国别查询
     * @param price               //价格
     * @return 返回轿车车系名称 返回Suv车系名称 返回其他车系名称
     */
    @RequestMapping("/select_index_heand_levei_series/{different_countries}/{price}")
    public Map<String, Object> select_index_heand_levei_series(@PathVariable Integer different_countries, @PathVariable String price) {
        return car_seriesService.select_index_heand_levei_series(different_countries, price);
    }

    /**
     * 查询同级车排行--5辆
     *
     * @param conditions_type            条件类型（销量/热门度/懂车分）
     * @param car_series_id              车系id
     * @param car_series_car_level_fk_id 该车系级别id
     * @return
     */
    @RequestMapping("/select_same_level_ranking/{conditions_type}/{car_series_id}/{car_series_car_level_fk_id}")
    public List<Car_series> select_same_level_ranking(@PathVariable String conditions_type, @PathVariable int car_series_id, @PathVariable int car_series_car_level_fk_id) {
        return car_seriesService.select_same_level_ranking(conditions_type, car_series_id, car_series_car_level_fk_id);
    }

    /**
     * 查询首页新车
     *
     * @return 返回 名称 时间 最低价格 最高价格 图片 资讯id 资讯标题
     */
    @RequestMapping("/select_index_new_series")
    public List<Car_series> select_index_new_series() {
        return car_seriesService.select_index_new_series();
    }

    /**
     * 查询车系名称 品牌名称
     *
     * @param car_series_id 车系id
     * @return
     */
    @RequestMapping("/select_series_id_brand/{car_series_id}")
    public Car_series select_series_id_brand(@PathVariable Integer car_series_id) {
        return car_seriesService.select_series_id_brand(car_series_id);
    }

    /**
     * 查询车系视频推荐排行榜
     *
     * @return
     */
    @RequestMapping("/select_series_video_recommend_ranking")
    List<Car_series> select_series_video_recommend_ranking() {
        return car_seriesService.select_series_video_recommend_ranking();
    }

    /**
     * 排行榜分页查询所有(根据销量排行)
     *
     * @param pageRankingIndex 当前页
     * @return map返回数据
     */
    @RequestMapping("/select_sales_all_series/{pageRankingIndex}/{brand_id}/{car_level_id}/{newtime}/{min_price}/{max_price}")
    public Map<String, Object> select_sales_all_series(@PathVariable Integer pageRankingIndex, @PathVariable Integer brand_id, @PathVariable Integer car_level_id, @PathVariable Integer newtime, @PathVariable Integer min_price, @PathVariable Integer max_price) {
        Map<String, Object> map = new HashMap<>();
        //查询所有品牌(排行榜)
        //map.put("select_brandAll",brandService.select_brandAll());
        map.put("select_sales_all_series", car_seriesService.select_sales_all_series(pageRankingIndex, brand_id, car_level_id, newtime, min_price, max_price));
        return map;
    }

    /**
     * 查询相关车系
     * 返回车系id  平均分  产商指导价  最高降幅  品牌名称 品牌图片
     *
     * @param car_series_id
     * @return
     */
    @RequestMapping("/select_interrelated_series/{car_series_id}")
    public List<Car_series> select_interrelated_series(@PathVariable int car_series_id) {
        return car_seriesService.select_interrelated_series(car_series_id);
    }

    /**
     * 热销榜(排行榜)
     *
     * @return
     */
    @RequestMapping("/select_hot_ranking")
    public List<Car_series> select_hot_ranking() {
        return car_seriesService.select_hot_ranking();
    }

    /**
     * 降价榜(排行榜)
     *
     * @return
     */
    @RequestMapping("/select_cut_price_ranking")
    public List<Car_series> select_cut_price_ranking() {
        return car_seriesService.select_cut_price_ranking();
    }

    /**
     * 搜索查询车系
     *
     * @param solr_content 搜索文本内容
     * @param page_index   当前页数
     * @return
     */
    @RequestMapping("/select_car_series_by_solr/{solr_content}/{page_index}")
    public Map<String, Object> select_car_series_by_solr(@PathVariable String solr_content, @PathVariable int page_index) throws IOException, SolrServerException {
        return car_seriesService.select_car_series_by_solr(solr_content, page_index);
    }

    /**
     * 根据热门度查询所有车系名称在搜索文本框中显示
     *
     * @return
     */
    @RequestMapping("/select_all_car_series_name")
    public List<Car_series> select_all_car_series_name() {
        return car_seriesService.select_all_car_series_name();
    }


}
